package cn.flying.cloud.system.user.controller;

import javax.annotation.Resource;

import java.util.List;
import java.util.Objects;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.flying.cloud.base.common.vo.PageRequest;
import cn.flying.cloud.base.common.vo.PageResponse;
import cn.flying.cloud.base.common.vo.Rt;
import cn.flying.cloud.system.user.entity.SysUserRole;
import cn.flying.cloud.system.user.service.SysUserRoleService;

/**
 * 用户角色表 前端控制器
 *
 * @author: admin
 * @date: 2023年04月19日 16:27
 * @version: 1.0
 */
@RestController
@RequestMapping("/system/user/role")
public class SysUserRoleController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private SysUserRoleService sysUserRoleService;

    /**
     * 分页查询用户角色表数据
     *
     * @param request
     * @return
     */
    @PostMapping("page")
    public PageResponse<SysUserRole> page(@RequestBody PageRequest<SysUserRole> request) {
        return sysUserRoleService.getPageList(request);
    }

    /**
     * 列表查询用户角色表数据
     *
     * @param sysUserRole
     * @return
     */
    @PostMapping("list")
    public Rt<?> list(@RequestBody SysUserRole sysUserRole) {
        List<SysUserRole> list = sysUserRoleService.getList(sysUserRole);
        return Rt.success(list);
    }

    /**
     * 查询用户角色表详情数据
     *
     * @param sysUserRole
     * @return
     */
    @PostMapping("detail")
    public Rt<?> detail(SysUserRole sysUserRole) {
        return Rt.success(sysUserRoleService.getOneObj(sysUserRole));
    }

    /**
     * 根据主键ID查询用户角色表详情数据
     *
     * @param id
     * @return
     */
    @PostMapping("getById")
    public Rt<?> getById(Long id) {
        SysUserRole sysUserRole = sysUserRoleService.getById(id);
        return Rt.success(sysUserRole);
    }

    /**
     * 保存用户角色表数据
     *
     * @param sysUserRole
     * @return
     */
    @PostMapping("save")
    public Rt<?> save(@RequestBody SysUserRole sysUserRole) {
        if (Objects.isNull(sysUserRole.getId())) {
            // 新增
            sysUserRoleService.createObj(sysUserRole);
        } else {
            // 更新
            sysUserRoleService.updateObj(sysUserRole);
        }
        return Rt.success("保存成功！");
    }

    /**
     * 删除用户角色表数据
     *
     * @param sysUserRole
     * @return
     */
    @PostMapping("delete")
    public Rt<?> delete(SysUserRole sysUserRole) {
        sysUserRoleService.deleteObj(sysUserRole);
        return Rt.success("删除成功！");
    }
}
